﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class LookupHelpers
    {
        public static MvcHtmlString LookupFor<T, TReturn>(
            this HtmlHelper<T> html,
            Expression<Func<T, TReturn>> prop,
            object value = null,
            string controller = null,
            string title = null,
            int? height = null,
            int? width = null,
            string chooseText = null,
            string cancelText = null,
            bool? clearButton = null,
            object htmlAttributes = null,
            bool? multiselect = null,
            bool? fullscreen = null,
            string prefix = null,
            bool? paging = null,
            IDictionary<string, string> data = null,
            IDictionary<string, object> parameters = null,
            string area = null)
        {
            return Lookup(html, ExpressionHelper.GetExpressionText(prop), value, controller, title, height, width, chooseText, cancelText, clearButton, htmlAttributes, multiselect, fullscreen, prefix, paging, data, parameters, area);
        }

        public static MvcHtmlString Lookup(this HtmlHelper html, LookupParams o)
        {
            o.ReadMeta(html);
            o.Value = o.Value.FromModel(html, o.Prop);

            if (Settings.GetText != null)
            {
                if (o.CancelText == null) o.CancelText = Settings.GetText("Lookup", "Cancel");
                if (o.ChooseText == null) o.ChooseText = Settings.GetText("Lookup", "Choose");
                if (o.Title == null) o.Title = Settings.GetText("Lookup", "Title");
            }
            
            o.ReadSettings(typeof(Settings.Lookup));
            o.Controller = o.Controller.DefaultController(o.Prop, "Lookup");
            var info = new LookupInfo();
            
            info.ReadParams(o);

            var hd = o.HtmlAttributes.GetDictionary();

            if (!html.ViewData.ModelState.IsValidField(o.Prop)) hd = hd.AddVal("class", "input-validation-error");
            if (info.Multiselect) hd = hd.AddVal("class", "ae-lookup-multidisplay");
            info.HtmlAttributes = hd.ToHtml();

            return html.Partial(Settings.AwesomeFolder + (info.Multiselect ? "MultiLookup.ascx" : "Lookup.ascx"), info);
        }

        public static MvcHtmlString Lookup(
            this HtmlHelper html,
            string prop,
            object value = null,
            string controller = null,
            string title = null,
            int? height = null,
            int? width = null,
            string chooseText = null,
            string cancelText = null,
            bool? clearButton = null,
            object htmlAttributes = null,
            bool? multiselect = null,
            bool? fullscreen = null,
            string prefix = null,
            bool? paging = null,
            IDictionary<string, string> data = null,
            IDictionary<string, object> parameters = null,
            string area = null)
        {
            return
                html.Lookup(new LookupParams
                                {
                                    CancelText = cancelText,
                                    ChooseText = chooseText,
                                    ClearButton = clearButton,
                                    Controller = controller,
                                    Data = data,
                                    Fullscreen = fullscreen,
                                    Height = height,
                                    HtmlAttributes = htmlAttributes,
                                    Multiselect = multiselect,
                                    Paging = paging,
                                    Parameters = parameters,
                                    Prefix = prefix,
                                    Prop = prop,
                                    Title = title,
                                    Value = value,
                                    Width = width,
                                    Area = area
                                });
        }
    }
}
